package com.czp.menu.action;

import com.czp.framework.action.BaseActionSupport;
import com.czp.framework.exception.IServiceException;
import com.czp.menu.model.Icon;
import com.czp.menu.model.Menu;
import com.czp.menu.service.IMenuService;

/**
 * 【环 境】：J2SE 1.7
 * @author   柴志鹏
 * @version  version 1.0
 * @since    2014-5-12
 */
public class MenuAction extends BaseActionSupport {
	private static final long serialVersionUID = 1891502338117705680L;
	private IMenuService menuService;
	private Menu menu;
	
	/** 
	 * 【描 述】：获取用户菜单列表
	 */
	public void getMenuList() { 
		try {
			String personId = request.getParameter("personId");
			// 取出全部数据集合并以Json形势返回
			this.responseOutPrint(menuService.getMenuList(personId));
		} catch (Exception e) {
			throw new IServiceException(this.getClass() + "getMenuList", e);
		}
	}
	
	/** 
	 * 【描 述】：查询"设置菜单"结果集
	 */
	public void getSettingsListByPid() {
		try {
			this.responseOutPrint(
					menuService.getSettingsListByPid(
							request.getParameter("pid")));
		} catch(Exception e) {
			throw new IServiceException(this.getClass() + "getSettingsListByPid", e);
		}
	}
	
	/** 
	 * 【描 述】： 根据父ID查询"用户菜单"节点数据
	 */
	public void getMenuTreeByPid() {
		try {
			Menu menu = new Menu();
			menu.setPid(request.getParameter("pid"));
			menu.setMenuType(request.getParameter("menuType"));
			this.responseOutPrint(
					menuService.getMenuTreeByPid(menu));
		} catch(Exception e) {
			throw new IServiceException(this.getClass() + "getMenuTreeByPid", e);
		}
	}
	
	/** 
	 * 【描 述】：添加、修改、删除菜单节点
	 */
	public void menuOperator() {
		try {
			menu.setCudType(request.getParameter("cudType"));
			Integer count = menuService.menuOperator(menu);
			this.responseOutPrint(
					"{success: "+ count +"}");
		} catch(Exception e) {
			try {
				this.responseOutPrint("{success: 0}");
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			throw new IServiceException(this.getClass() + "menuOperator", e);
		}
	}
	
	/**
	 * 【描 述】：排序菜单节点
	 */
	public void sortMenuNode() {
		try {
			String jsonData = request.getParameter("jsonData");
			String pid = request.getParameter("parentCode");
			Integer count = menuService.sortMenuNode(jsonData, pid);
			this.responseOutPrint(
					"{success: "+ count +"}");
		} catch(Exception e) {
			throw new IServiceException(this.getClass() + "sortMenuNode", e);
		}
	}
	
	/** 
	 * 【描 述】：更新父菜单节点leaf状态
	 */
	public void updateParentLeaf() {
		try {
			Integer count = menuService.updateParentLeaf(request.getParameter("parentCode"));
			this.responseOutPrint(
					"{success: "+ count +"}");
		} catch(Exception e) {
			throw new IServiceException(this.getClass() + "updateParentLeaf", e);
		}
	}
	
	/** 
	 * 【描 述】：查询Icon图片样式
	 */
	public void queryMenuIcon() {
		Icon icon = new Icon();
		try {
			icon.setStart(Integer.parseInt(request.getParameter("start")));
			icon.setLimit(Integer.parseInt(request.getParameter("limit")));
			this.responseOutPrint(
					menuService.queryMenuIcon(icon));
		} catch(Exception e) {
			throw new IServiceException(this.getClass()+ "querymenuIcon", e);
		}
	}
	
	/** 
	 * 【描 述】：查询根节点信息
	 */
	public void getMenuRootList() {
		try {
			responseOutPrint(menuService.getMenuRootList());
		} catch(Exception e) {
			throw new IServiceException(this.getClass()+ "getMenuRootList", e);
		}
	}
	
	/** 
	 * 【描 述】： 根据父ID查询"用户菜单"节点数据
	 */
	public void getMenuLeafByPid() {
		try {
			String pid = request.getParameter("pid");
			this.responseOutPrint(
					menuService.getMenuLeafByPid(pid));
		} catch(Exception e) {
			throw new IServiceException(this.getClass() + "getMenuLeafByPid", e);
		}
	}
	
	/**
	 * 获取 menuService
	 * @return 返回 menuService
	 */
	public IMenuService getMenuService() {
		return menuService;
	}
	/**
	 * 设置 menuService
	 * @param 对menuService进行赋值
	 */
	public void setMenuService(IMenuService menuService) {
		this.menuService = menuService;
	}

	/**
	 * 获取 menu
	 * @return 返回 menu
	 */
	public Menu getMenu() {
		return menu;
	}

	/**
	 * 设置 menu
	 * @param 对menu进行赋值
	 */
	public void setMenu(Menu menu) {
		this.menu = menu;
	}
	
}
